//package com.xzjie.cms.configure;
//
//import com.xzjie.cms.core.utils.JsonUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.quartz.Scheduler;
//import org.quartz.SchedulerException;
//import org.quartz.SchedulerFactory;
//import org.quartz.ee.servlet.QuartzInitializerListener;
//import org.quartz.impl.StdSchedulerFactory;
//import org.quartz.spi.JobFactory;
//import org.quartz.spi.TriggerFiredBundle;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
//import org.springframework.beans.factory.config.PropertiesFactoryBean;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContextAware;
//import org.springframework.context.ApplicationListener;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.event.ContextRefreshedEvent;
//import org.springframework.core.io.ClassPathResource;
//import org.springframework.scheduling.quartz.SchedulerFactoryBean;
//import org.springframework.scheduling.quartz.SpringBeanJobFactory;
//
//import javax.sql.DataSource;
//import java.io.IOException;
//import java.util.Properties;
//
//@Slf4j
//@Configuration
//public class QuartzConfigure implements ApplicationListener<ContextRefreshedEvent> {
////    @Autowired
////    private DataSource dataSource;
//
//    //配置JobFactory
//    @Bean
//    public JobFactory jobFactory(ApplicationContext applicationContext) {
//        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
//        jobFactory.setApplicationContext(applicationContext);
//        return jobFactory;
//    }
//
////    @Bean(name="scheduler")
////    public Scheduler scheduler() throws SchedulerException, IOException {
//////        SchedulerFactory factory = new StdSchedulerFactory();
//////        Scheduler scheduler = factory.getScheduler();
//////        scheduler.start();
//////        return scheduler;
////        return schedulerFactoryBean().getScheduler();
////    }
//
//    @Bean
//    public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean) throws SchedulerException {
//        Scheduler scheduler = schedulerFactoryBean.getScheduler();
//        scheduler.start();
//        return scheduler;
//    }
//
//    @Override
//    public void onApplicationEvent(ContextRefreshedEvent event) {
//        log.info("Quartz 任务已经启动...");
//        log.info(">>>>:" + event.getSource());
//    }
//
//
//    /**
//     * SchedulerFactoryBean这个类的真正作用提供了对org.quartz.Scheduler的创建与配置，并且会管理它的生命周期与Spring同步。
//     * org.quartz.Scheduler: 调度器。所有的调度都是由它控制。
//     *
//     * @param jobFactory 为SchedulerFactory配置JobFactory
//     */
//    @Bean
//    public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource, JobFactory jobFactory) throws IOException {
//        SchedulerFactoryBean factory = new SchedulerFactoryBean();
//        //可选,QuartzScheduler启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录
//        factory.setOverwriteExistingJobs(true);
//        factory.setAutoStartup(true); //设置自行启动
//        factory.setDataSource(dataSource);
//        factory.setJobFactory(jobFactory);
//        // 这样当spring关闭时，会等待所有已经启动的quartz job结束后spring才能完全shutdown。
//        factory.setWaitForJobsToCompleteOnShutdown(true);
//        factory.setQuartzProperties(quartzProperties());
//        return factory;
//    }
////    @Bean
////    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
////        //
////        //创建SchedulerFactoryBean
////        SchedulerFactoryBean factory = new SchedulerFactoryBean();
////        factory.setQuartzProperties(quartzProperties());
////        //使用数据源，自定义数据源
////        factory.setDataSource(this.dataSource);
////        factory.setJobFactory(this.jobFactory);
////        factory.setWaitForJobsToCompleteOnShutdown(true);//这样当spring关闭时，会等待所有已经启动的quartz job结束后spring才能完全shutdown。
////        factory.setOverwriteExistingJobs(false);
////        factory.setStartupDelay(1);
////        return factory;
////    }
//
//    /**
//     * 获取配置属性
//     *
//     * @return
//     * @throws IOException
//     */
//    @Bean
//    public Properties quartzProperties() throws IOException {
//        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
//        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
//        //在quartz.properties中的属性被读取并注入后再初始化对象
//        propertiesFactoryBean.afterPropertiesSet();
//        return propertiesFactoryBean.getObject();
//
//    }
//
////    @Bean
////    public QuartzInitializerListener executorListener() {
////        return new QuartzInitializerListener();
////    }
//
//
//    //配置JobFactory,为quartz作业添加自动连接支持
//    public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {
//        /**
//         * AutowireCapableBeanFactory接口是BeanFactory的子类
//         * 可以连接和填充那些生命周期不被Spring管理的已存在的bean实例
//         */
//        private AutowireCapableBeanFactory beanFactory;
//
//        @Override
//        public void setApplicationContext(final ApplicationContext context) {
//            beanFactory = context.getAutowireCapableBeanFactory();
//        }
//
//        /**
//         * 创建Job实例
//         *
//         * @param bundle
//         * @return
//         * @throws Exception
//         */
//        @Override
//        protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
//            final Object job = super.createJobInstance(bundle);
//            beanFactory.autowireBean(job);
//            return job;
//        }
//    }
//
//}
